import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import UiManager from "./UiManage";

import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./AbTestMgr"
import c from "./OutsideGuideExperimentMgr"
import u from "./PeripheralSystemDataMgr"
import p from "./EventMgr"
import d from "./GameEventType"
import h from "./MatchGameEventType"
import f from "./frameworkManager"
import g from "./AdManager"
import y from "./SdkHelper"
import m from "./EngineUtil"
import _ from "./MatchGameData"
import v from "./BasePageCtrl"
import b from "./PageMgr"
import C, { OutSideLevelGuide } from "./PeripheralSystemMgr"
import T from "./UiManage"
import M from "./MatchGameMoneyPage"
import MatchGameGoldShop from "./MatchGameGoldShop";
import GameService from "./GameService";
import Handler from "./Handler";
import MatchGameData from "./MatchGameData";
import PageMgr from "./PageMgr";
import PlayerDataSys from "./PlayerDataSys";

interface IStoreDetail {
    id?: number;
    configId?: number;
    propId?: number;
    propName?: string;
    propDescription?: string;
    prize?: number;
    propNum?: number;
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class MatchGameGoldShopCtrl extends BasePageCtrl {

    public storeDetail: IStoreDetail[] = [];
    onLoad() {
        this.onUILoad();
        this.addButtonListen();
        super.onLoad();
    }

    onUILoad() {
        this.ui = this.node.addComponent(MatchGameGoldShop);
    }
    public _onShow(): void {
        super._onShow();

        this.__init();
    }

    __init() {
        GameService.gameConfig(
            Handler.create(this, (t) => {
                this.storeDetail = t.storeDetails;
                for (let i = 0; i < 3; i++) {
                    this.ui[`item${i + 1}`].active = false;
                }

                for (let i = 0; i < this.storeDetail.length; i++) {
                    this.ui[`item${i + 1}`].active = true;
                    (this.ui[`item${i + 1}_name`] as cc.Node).getComponent(cc.Label).string = this.storeDetail[i].propName + "x" + this.storeDetail[i].propNum;
                    (this.ui[`item${i + 1}_gold`] as cc.Node).getComponent(cc.Label).string = this.storeDetail[i].prize + "";
                    (this.ui[`item${i + 1}_store`] as cc.Node).getComponent(cc.Label).string = MatchGameData.getPropByType(this.storeDetail[i].propId).skillCount + "";
                    (this.ui[`item${i + 1}_info`] as cc.Node).getComponent(cc.Label).string = this.storeDetail[i].propDescription;
                    (this.ui[`item${i + 1}_btn`] as cc.Node).off('click');
                    UiManager.addButtonListen(this.ui[`item${i + 1}_btn`], () => {
                        this.itemClickHandler(this.storeDetail[i].id,i)
                    }, this);
                }
            }),
            Handler.create(this, (e) => {

            })
        );
    }

    addButtonListen() {
        UiManager.addButtonListen(this.ui.closeBtn, this.clickClose, this);
    }

    private itemClickHandler(id,index) {
        GameService.storeConsume(
            Handler.create(this, (t) => {
                console.log(JSON.stringify(t));
                if (t.code == 0) {
                    PageMgr.showPage("TravelToastPage", {
                        text: "购买成功"
                    });
                    PlayerDataSys.updateGoldBalace(t.result.haveGold);
                    MatchGameData.getPropByType(this.storeDetail[index].propId).skillCount++;
                    (this.ui[`item${index + 1}_store`] as cc.Node).getComponent(cc.Label).string = MatchGameData.getPropByType(this.storeDetail[index].propId).skillCount + "";
                } else {
                    console.log(JSON.stringify(t));
                    PageMgr.showPage("TravelToastPage", {
                        text: t.errorMsg
                    });
                }
            }),
            id
        );
    }

    clickClose() {
        this.hide();
    }
}
